/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package GuiTools;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

public class EditButton extends JButton{
	String txt;
	boolean enabled;
	Color FONT = Color.black;
	JButton btn;
	Image image,hlImage,disabledImage,enabledImage;
	public EditButton(Image img, Image hlImg, Image dImage, boolean argEnabled){
		this("image",argEnabled);
		image = img;
		hlImage = hlImg;
		disabledImage = dImage;
		enabledImage = image;
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				btn.repaint();
			}
		});
	}
	/**
	 * 
	 * @param argTxt button text
	 * @param argEnabled enabled
	 */
	public EditButton(String argTxt, boolean argEnabled){
		super();
		btn=this;
		this.setUI(null);
		this.setBorder(null);
		this.addMouseListener(new MouseAdapter(){
			
			public void mouseClicked(MouseEvent e) {
				btn.doClick();
			}

			public void mouseEntered(MouseEvent e) {
				FONT = Color.red;
				enabledImage = hlImage;
				SwingUtilities.invokeLater(new Runnable(){
					public void run(){
						btn.repaint();
					}
				});
			}

			public void mouseExited(MouseEvent e) {
				FONT = Color.black;
				enabledImage = image;
				SwingUtilities.invokeLater(new Runnable(){
					public void run(){
						btn.repaint();
					}
				});
			}
		});
		setOpaque(true);
		txt = argTxt;
		enabled=argEnabled;
		this.setEnabled(enabled);
		setPreferredSize(new Dimension(30,30));
	}
	
	public void paintComponent(Graphics g){
		int step = 3;
		int x1=10;
		int x2=20;
		for(int i=0;i<30;i++){
			int col = (i*step) + 165;
			g.setColor(new Color(col,col,col));
			g.drawLine(x1,i,x2,i);
			if(i<5){x1 = x1-2;x2=x2+2;}
			if(i>24){x1=x1+2;x2=x2-2;}
		}
		if(this.isEnabled()) g.setColor(FONT);
		else g.setColor(Color.gray);
		if(txt.equals("image")){
			if(this.isEnabled())g.drawImage(enabledImage,0,0,null);
			else {
				g.drawImage(disabledImage,0,0,null);
			}
			return;
		}
		if(txt.equals("edt")){
			for(int i = 0;i<2;i++){
				g.drawLine(10+i, 20,17+i,13);
			}
			for(int i=0;i<3;i++){
				g.drawLine(15+i, 11-i, 20+i, 16-i);
			}
			g.drawLine(16,11,21,16);
			g.drawLine(17,10,22,15);
			return;
		}
		if(txt.equals("name")){
			g.drawLine(12,10,12,20);
			g.drawLine(11,10,11,20);
			g.drawLine(18,10,18,20);
			g.drawLine(17,10,17,20);
			g.drawLine(11, 10, 17, 20);
			g.drawLine(12, 10, 18, 20);
			g.fillOval(20,18,3,3);
			return;
		}
		if(txt.equals("-")){
			g.drawLine(10, 14, 20, 14);
			g.drawLine(10, 15, 20, 15);
			return;
		}
		if(txt.equals("+")){
			g.drawLine(10, 14, 20, 14);
			g.drawLine(10, 15, 20, 15);
			g.drawLine(14,10,14,20);
			g.drawLine(15,10,15,20);
			return;
		}
		if(txt.equals("++")){
			g.drawLine(4,14,14,14);
			g.drawLine(4,15,14,15);
			g.drawLine(16,14,26,14);
			g.drawLine(16,15,26,15);
			
			g.drawLine(9, 10, 9, 20);
			g.drawLine(10, 10, 10, 20);
			g.drawLine(20, 10, 20, 20);
			g.drawLine(21, 10, 21, 20);
		}
		else{
			g.setFont(new Font(Font.SERIF,Font.BOLD,20));
			g.drawString(txt,10,22);
		}
	}
}